﻿package mortal.game.view.daily.view
{
    import Message.DB.Tables.*;
    import com.gengine.core.frame.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.pools.*;
    import frEngine.primitives.object2d.*;
    import mortal.common.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.model.data.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.view.common.*;

    public class DailyBossShowView extends DailyShowBaseView
    {
        private var _intr:GTextFiled;
        private var _name:GTextFiled;
        private var _rotationLeftBtn:GLoadedButton;
        private var _rotationRightBtn:GLoadedButton;
        private var _frameTimer:FrameTimer;
        private var _window:Window;
        private var turnValue:int;
        private var _rect3d:Rect3DObject;
        private var _bodyPlayer:ActionPlayer;
        private var _img2d:Img2D;
        private static var _instance:DailyBossShowView;

        public function DailyBossShowView()
        {
            return;
        }// end function

        protected function onClickTurnBtn(event:MouseEvent) : void
        {
            if (this._bodyPlayer)
            {
                if (event.currentTarget == this._rotationLeftBtn)
                {
                    this.turnValue = 2;
                }
                if (event.currentTarget == this._rotationRightBtn)
                {
                    this.turnValue = -2;
                }
                this.start();
            }
            return;
        }// end function

        private function start() : void
        {
            if (!this._frameTimer)
            {
                this._frameTimer = new FrameTimer(1, int.MAX_VALUE, true);
                this._frameTimer.addListener(TimerType.ENTERFRAME, this.onTurning);
            }
            this._frameTimer.start();
            return;
        }// end function

        protected function onTurning(param1:FrameTimer) : void
        {
            if (this._bodyPlayer)
            {
                this._bodyPlayer.rotationY = this._bodyPlayer.rotationY + this.turnValue;
            }
            return;
        }// end function

        protected function stopTurning(event:MouseEvent = null) : void
        {
            if (this._frameTimer)
            {
                this._frameTimer.stop();
            }
            return;
        }// end function

        private function add3dModle() : void
        {
            if (_data && _isLoader)
            {
                DisplayUtil.removeMe(_bg);
                this._rect3d = Rect3DManager.instance.registerWindow(true, new Rectangle(431, 63, 397, 278), this._window);
                Rect3DManager.instance.windowShowHander(null, this._window);
                this._rect3d.removeImg(this._img2d);
                this._img2d = new Img2D(null, _bg.bitmapData, new Rectangle(0, 0, 397, 278), false);
                this._rect3d.addImg(this._img2d);
                this._rect3d.disposeObj3d(this._bodyPlayer);
                this._bodyPlayer = FrObjectPool.getObject(ActionPlayer, this);
                this._bodyPlayer.changeAction(ActionName.Stand);
                this._bodyPlayer.hangBoneName = BoneConst.MountHangModelBone;
                this._bodyPlayer.selectEnabled = false;
                this._bodyPlayer.play();
                this._bodyPlayer.setRotation(0, 0, 0);
                this._bodyPlayer.load(_data.bossId + ".md5mesh", _data.bossId + ".skeleton", _data.bossId + ".abc");
                this._rect3d.addObject3d(this._bodyPlayer, 200, 200);
            }
            return;
        }// end function

        public function set window(param1:Window) : void
        {
            this._window = param1;
            return;
        }// end function

        override protected function loadBgCom() : void
        {
            super.loadBgCom();
            this.add3dModle();
            return;
        }// end function

        override public function set data(param1:TDailyViewConfig) : void
        {
            super.data = param1;
            this.add3dModle();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._intr = UIFactory.gTextField("", 50, 50, 220, 120, this);
            this._intr.multiline = true;
            this._intr.wordWrap = true;
            this._name = UIFactory.gTextField("", 30, 30, 220, 20, this);
            this._rotationLeftBtn = UIFactory.gLoadedButton(ImagesConst.TurnLeft_upSkin, 247, 175, 40, 36, this);
            this._rotationRightBtn = UIFactory.gLoadedButton(ImagesConst.TurnRight_upSkin, 105, 179, 40, 36, this);
            this._rotationLeftBtn.configEventListener(MouseEvent.MOUSE_DOWN, this.onClickTurnBtn);
            this._rotationRightBtn.configEventListener(MouseEvent.MOUSE_DOWN, this.onClickTurnBtn);
            Global.stage.addEventListener(MouseEvent.MOUSE_UP, this.stopTurning);
            this.bgUrl = ImagesConst.DailyBossBg;
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            Global.stage.removeEventListener(MouseEvent.MOUSE_UP, this.stopTurning);
            this._intr.dispose(param1);
            this._name.dispose(param1);
            this._rotationLeftBtn.dispose(param1);
            this._rotationRightBtn.dispose(param1);
            if (this._frameTimer)
            {
                this._frameTimer.dispose(param1);
            }
            if (this._rect3d)
            {
                Rect3DManager.instance.disposeRect3d(this._rect3d);
                this._rect3d = null;
                this._img2d = null;
                this._bodyPlayer = null;
            }
            this._frameTimer = null;
            this._window = null;
            return;
        }// end function

        public static function get instance() : DailyBossShowView
        {
            if (!_instance)
            {
                _instance = new DailyBossShowView;
            }
            return _instance;
        }// end function

        public static function get hasInstance() : Boolean
        {
            return _instance != null;
        }// end function

    }
}
